import prompt from '@system.prompt';

const ActionParam = {
    ABILITY_TYPE_EXTERNAL : 0,
    ABILITY_TYPE_INTERNAL : 1,
// syncOption(Optional, default sync): 0-Sync; 1-Async
    ACTION_SYNC : 0,
    ACTION_ASYNC : 1,
}

function showToast(message) {
    var messageOb = {
        "message" : message,
        "duration" : 1500
    }
    prompt.showToast(messageOb);
}

function newActionBuilder() {
    var actionBuilder = {
        action : {},
        setBundleName : function(bundleName) {
            this.action.bundleName = bundleName;
            return this;
        },
        setAbilityName : function(abilityName) {
            this.action.abilityName = abilityName;
            return this;
        },
        setMessageCode : function(messageCode) {
            this.action.messageCode = messageCode;
            return this;
        },
        setAbilityType : function(abilityType) {
            this.action.abilityType = abilityType;
            return this;
        },
        setSyncOption : function(syncOption) {
            this.action.syncOption = syncOption;
            return this;
        },
        setData : function(data) {
            this.action.data = data;
            return this;
        },
        getAction() {
            return this.action;
        }
    }
    return actionBuilder;
}

const Action = {
    FLOWTO : 1001,
    FULLSCREEN : 1002,
    EXITFULLSCREEN : 1003,
    SAVE_CTX : 1004,
    LOAD_CTX : 1005
}

export default {
    global : {
        video_list: [
            {
                "title" : "0",
                "url" : "/common/demo/HM_demo.mp4"
            },
            {
                "title" : "1",
                "url" : "http://192.168.3.32/test.mp4"
            },
            {
                "title" : "2",
                "url" : "http://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4"
            },
            {
                "title" : "3",
                "url" : "https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218093206z8V1JuPlpe.mp4"
            },
            {
                "title" : "4",
                "url" : "https://stream7.iqilu.com/10339/article/202002/18/2fca1c77730e54c7b500573c2437003f.mp4"
            },

        ],
        comments: [],
        video_index : 0,
        current_time : 0,
        input_comment : "",
        input_url : ""
    },
    loading_promise : null,
    async load_ctx() {
        var actionData = {}
        var actionBuilder = newActionBuilder();
        var action = actionBuilder.setBundleName("top.january.codelabf2")
            .setAbilityName("top.january.codelabf2.InMainAbility")
            .setAbilityType(ActionParam.ABILITY_TYPE_INTERNAL)
            .setSyncOption(ActionParam.ACTION_SYNC)
            .setMessageCode(Action.LOAD_CTX)
            .setData(actionData)
            .getAction();
        var result = await FeatureAbility.callAbility(action);
        if (result.length > 10) {
            this.global = JSON.parse(result);
        }
        return 0;
    },
    async save_ctx() {
        var actionBuilder = newActionBuilder();
        showToast(this.global);
        var action = actionBuilder.setBundleName("top.january.codelabf2")
            .setAbilityName("top.january.codelabf2.InMainAbility")
            .setAbilityType(ActionParam.ABILITY_TYPE_INTERNAL)
            .setSyncOption(ActionParam.ACTION_SYNC)
            .setMessageCode(Action.SAVE_CTX)
            .setData(this.global)
            .getAction();
        return FeatureAbility.callAbility(action);
    },
    onReady() {
        console.info('AceApplication onReady');
    },
    onCreate() {
        this.loading_promise = this.load_ctx();
        console.info('AceApplication onCreate');

    },
    onDestroy() {
        // 这个调用似乎有概率无法完成
        this.save_ctx();
        console.info('AceApplication onDestroy');
    }
};
